/**
 * 
 */

$(document).ready(function() {
	
	//retrieve user information
	updateUserData();
	
	// handle events
	$("#btn-user-logout").click(function() {
		logoutUser();
	});
	
});

function updateUserData() {
	var userId = $.getCookie("geoserverudima.userId");
	var sessionToken = $.getCookie("geoserverudima.sessionToken");
	
	$.ajax({
		url : "../rest/users/" + userId,
		type : "GET",
		success : function(data) {
			paintHelloHeader(data);
		}
	});
}

function paintHelloHeader(user) {
	$("#div-hello-user-text").html("Hola, " + user.firstName + " " + user.lastName + " (" + user.userName + ")");
}

function logoutUser() {
	$.ajax({
	    url : "../rest/users/logout",
	    type: "POST",
	    contentType: "application/json",
	    dataType: "json",
	    data: JSON.stringify({
		    	"userId": $.getCookie("geoserverudima.userId"),
		    	"sessionToken": $.getCookie("geoserverudima.sessionToken")
	    		}
	    	),
	    success: function(data, textStatus, jqXHR) {
	        document.cookie = "geoserverudima.userId=''";
	        document.cookie = "geoserverudima.sessionToken=''";
	        window.location = "mobileEmulatorLogin.jsp";
	    },
	    error: function (jqXHR, textStatus, errorThrown) {
	 
	    }
	});
}

